package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.woniuxy.feign.UserFeign;
import com.woniuxy.mapper.ChatMapper;
import com.woniuxy.service.ChatService;
import com.work.framework.pojo.Chat;
import com.work.framework.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wanghao
 * @Date: 2019/9/19 17:34
 *
 * 处理买家与卖家聊天的业务逻辑层
 */
@Service
public class ChatServiceImpl implements ChatService {

    @Resource
    private ChatMapper chatMapper;
    @Resource
    private UserFeign userFeign;
    /**
     * 在chat数据库中插入买家和卖家
     * @param chat
     * @return
     */
    @Override
    public Integer addChat(Chat chat) {

        //双方用户是否存在
        User buyUser = userFeign.findUser(chat.getB_uInfo().getUid());
        User sellerUser = userFeign.findUser(chat.getP_uInfo().getUid());
        if (buyUser == null || sellerUser == null) {
            return 0;
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("p_uid", chat.getP_uInfo().getUid());
        map.put("b_uid", chat.getB_uInfo().getUid());
        List<Chat> chats = chatMapper.selectChatByMap(map);
        if (chats == null || chats.size() != 0) {
            return -1;
        }
        return  chatMapper.insert(chat);
    }

    /**
     * 在chat数据库中软删除买家和卖家通讯录
     * @param cid
     * @return
     */
    @Override
    public Integer deleteChat(Integer cid) {
        Chat chat = new Chat();
        chat.setCid(cid);
        chat.setFlage(0);
        return chatMapper.updateById(chat);
    }

    /**
     * 在chat数据库中查找卖家的通讯录
     * @param p_uid
     * @return
     */
    @Override
    public List<Chat> findSeller(Integer p_uid) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("p_uid", p_uid);
        return chatMapper.selectChatByMap(map);
    }

    /**
     * 在chat数据库中查找买家的通讯录
     * @param b_uid
     * @return
     */
    @Override
    public List<Chat> findBuyer(Integer b_uid) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("b_uid", b_uid);
        return chatMapper.selectByMap(map);
    }
}
